package com.core.util;

import java.nio.charset.Charset;

import org.apache.shiro.codec.Base64;

/**
 * base64工具类
 * 
 * @author pt-chenqw
 *
 */
@SuppressWarnings("static-access")
public class Base64Utils {

	 private static final Base64 BASE64 = new Base64();
	    private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
	    private static final String PREFIX = "XXXXX";

	    public static String encode(String source) {
	        if (!isEmpty(source)) {
	            new Base64();
	            String target = PREFIX + source;
				byte[] bytes = BASE64.encode(target.getBytes(DEFAULT_CHARSET));
	            return new String(bytes, DEFAULT_CHARSET);
	        }

	        return source;
	    }
	    
	    public static String decode(String source) {
	        if (!isEmpty(source)) {
	            byte[] bytes = BASE64.decode(source.getBytes(DEFAULT_CHARSET));
	            String target = new String(bytes, DEFAULT_CHARSET);
	            return target.startsWith(PREFIX) ? target.substring(PREFIX.length()) : target;
	        }

	        return source;
	    }

	    private static boolean isEmpty(String str) {
	        return str == null || str.length() == 0;
	    }

	}
